<template>
	<view class="">
		<view class="one">
				<text>类型</text>
				<view class="one-rig">
					<text style="">
						<picker @change="bindPickerChange" name="type" :value="index" :range="array">
							<view class="uni-input" >{{array[index]}}</view>
						</picker>
					</text>
					<view class="one-rig-right">
		
					</view>
				</view>
			</view>
			<view class="two">
				<text>地址</text>
				<view class="one-rig">
					<text style="">
						<picker @change="bindPickerChange1" name="type" :value="index1" :range="array1">
							<view class="uni-input" >{{array1[index1]}}</view>
						</picker>
					</text>
					<view class="one-rig-right">
					</view>
				</view>
			</view>
			<view class="three">
				<text>联系电话</text>
				<view class="one-rig">
					<input type="text" value="" v-model="tel"/>
					<view class="one-rig-right">
					</view>
				</view>
			</view>
			<textarea placeholder="asdasdas" class="four" v-model="xiangqing"/>
				<view class="image" >
					<image src="../../../static/img/camera.png" @click="xiangji"></image>
					 <view class="tupian1" v-for="item in photo">
					 	<image :src="item" mode=""></image>
					 </view>
				</view>
			<button type="default" @click="baoxiu">确认报修</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				array: ['请选择问题类型'],
				index: 0,
				array1: ['请选择报修地址'],
				index1: 0,
				token: '',
				baoxiuleixing:'',
				baoxiudizhi:'',
				tel:'',
				photo:[],
				totalImg:[],
				xiangqing:'',	
			}
		},
		methods: {
			
			//报修类型的下标选择
			bindPickerChange: function(e) {
				 // console.log(11111, e.target.value)
				this.index = e.target.value
				this.baoxiuleixing = this.array[this.index]
				// console.log(this.baoxiuleixing )
			},
			//报修地址的下标选择
			bindPickerChange1: function(e) {
				 // console.log(222, e.target.value)
				this.index1 = e.target.value
				this.baoxiudizhi = this.array1[this.index1]
				// console.log(this.baoxiudizhi )
			},
			//报修类型
			leixin() {
				uni.request({
					url: 'http://estate.eshareedu.cn/estate/api/user/repairstype/list',
					method: 'GET',
					header: {
						Authorization: this.token,
					},
					success: (res => {
						console.log(res.data)
						if (res.data.errCode == 10000) {
							let list = res.data.data.list
							list.forEach(item => {
								this.array.push(item.name)
							})
						}
					})
				})
			},
			//报修地址
			dizhi() {
				uni.request({
					url: 'http://estate.eshareedu.cn/estate/api/user/houses/list',
					method: 'GET',
					header: {
						Authorization: this.token,
					},
					data: {

					},
					success: (res => {
						console.log(111, res.data)
						if (res.data.errCode == 10000) {
							let shuju = res.data.data.list
							shuju.forEach(item => {
								this.array1.push(item.building)
							})
						}
					})
				})
			},
			//确认报修
			baoxiu() {
				console.log(666,this.baoxiuleixing)
				uni.request({
					url: 'http://estate.eshareedu.cn/estate/api/user/repairs/add',
					method: 'POST',
					header: {
						Authorization: this.token
					},
					data: {
						id: 0,
						address:this.baoxiudizhi,
						content:this.xiangqing,
						tel: this.tel,
						type: this.index,
						state: 1,
						images:this.totalImg
					},
					success:(res=>{
						console.log(333,res.data)
						if(res.data.errCode == 10000){
							uni.showToast({
								title:'添加成功',
							})
							setTimeout(function(){
								uni.navigateTo({
									url:'../ProblemList/ProblemList'
								})
							},1500)
						}
						
					})
				})
			},
			//相机请求
			xiangji(){
				uni.chooseImage({
					count: 3,
					sizeType: ['original'],//可以指定是原图还是压缩图。或者二者都有
					sourceType: ['album'],//从相册选择
					success: (res) => {
						uni.showLoading({
							title:'上传中'
						})
						let filepath = res.tempFilePaths;
						if(this.photo.length < 3){
							for (let i = 0; i < filepath.length; i++){
								uni.uploadFile({
									url:'http://121.42.230.80/api/mini/upload',
									filePath:filepath[i],
									name:'file',
									formData:{
										'user': 'test'
									},
									success:(uploadFileRes)=>{
										let img = uploadFileRes.data
										this.photo.unshift(img)
										let arr = []
										//循环追加数组
										for(var i = 0; i < this.photo.length;i++){
											arr.push({
												'url': this.photo[i]
											})
										}
										this.totalImg = arr
										uni.hideLoading()//隐藏加载弹框
										uni.showToast({
											title:"上传成功",
											icon:'success',
											
										})
									}
								})
							}
						}
						// uni.hideLoading()
					}
				})
			}
		},
		onLoad() {
			this.token = uni.getStorageSync('token');
			this.leixin();
			this.dizhi();
		}
	}
</script>

<style lang="less">
	@import url(../../../static/css/Problem/AddProblem.less);
	
</style>
